from time import sleep
class ThermistorHandler:
    def __init__(self,controller):
        self.tool=0
        self.thermistors= {"Bed":None, "Extruder0":None, "Extruder1":None}
        self.controller=controller

    def getThermistors(self):
        for therm in self.controller.heatController.thermistors.iterkeys():
            self.thermistors[therm]=self.controller.heatController.thermistors[therm]

    def setTool(self,tool):
        self.tool=tool

    def setHoldingTempFast(self,therm,temp):
        self.thermistors[therm].setHoldingTemp(temp)
        return "ok"

    def setHoldingTempWait(self,therm,temp):
        self.setHoldingTempFast(therm,temp)
        return self.waitTemp(therm)
        

    def setBedTempFast(self,temp):
        self.setHoldingTempFast("Bed",temp)

    def reportTemp(self):
        toolTemp=str(self.controller.cachedTemp["Extruder0"])
        bedTemp=str(self.controller.cachedTemp["Bed"])
        self.controller.lastRetVal="T:"+toolTemp+" B:"+bedTemp

    def waitTemp(self,therm):
        holdTemp = self.thermistors[therm].getHoldingTemp()
        while (self.controller.cachedTemp[therm] < holdTemp):
            self.reportTemp()
            sleep(1)
        return "ok"
    
    def waitBedTemp(self,temp):
        self.thermistors["Bed"].setHoldingTemp(temp)
        return self.waitTemp("Bed")
        

    def setMaxTemp(self,therm,temp):
        self.thermistors[therm].MaxTemp=temp

    def setMinTemp(self,therm,temp):
        self.thermistors[therm].MinTemp=temp

    def getCurrentTemp(self,therm):
        curTemp = self.thermistors[therm].getCurrentTemp()
        self.controller.cachedTemp[therm]=curTemp
        return curTemp

    def getBedTemp(self):
        return self.getCurrentTemp("Bed")
